<template>
	<view>
		<uni-popup ref="popup" type="bottom">
			<view class="bg-white ">
				<uni-section title="切换校区" type="circle">
					<template v-slot:right>
						<view @click="close">关闭</view>
					</template>
				</uni-section>
				<view class="padding-bottom-lg padding-sm" style="padding-bottom: 120rpx;">
					<uni-forms class="margin-sm" labelWidth="160rpx">
						<uni-forms-item label="选择校区">
							<uni-data-checkbox style="width: 100%;" mode="button" v-model="tenantId"
								:localdata="statusList"></uni-data-checkbox>
						</uni-forms-item>
					</uni-forms>
					<button @click="switchOrg" class="login-btn cu-btn block bg-blue lg round">切换</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
	// import {
	// 	getOrgListbyuserid
	// } from "@/api/system/tenant/tenantManagement";
	// import {
	// 	getAllUser,
	// 	switchOrg
	// } from "@/api/system/user/userManagement";
	// import {
	// 	setcommon
	// } from '@/utils/common';
	// import {
	// 	findTreeAll
	// } from '@/api/tenantSet/source';
	// import {
	// 	findAllByCurriculumList
	// } from '@/api/system/curriculum/AxCurriculumManagementApi';
	export default {
		data() {
			return {
				tenantId: null,
				statusList: [],

			}
		},
		props: {
			customerId: null,

			customer: {}
		},
		methods: {
			open(data) {
				//加载校区
				this.getOrgListbyuserid()
				this.tenantId = this.$store.state.user.user.orgIdsCurrent
				this.$refs.popup.open()
			},
			// 选择组织
			async switchOrg() {
				const foundElement = this.statusList.find(item => item.id == this.tenantId);

				const {
					success,
					msg,
					data
				} = await switchOrg({
					tenantId: this.tenantId,
				});
				if (success) {
					setcommon("tenantInfo", foundElement)


					this.loginSuccess()

					window.location.reload();
				}
			},

			// 登录成功后，处理函数
			async loginSuccess(result) {


				//获取所有用户
				getAllUser().then((res) => {
					setcommon("createUserNameList", res.data);
				});

				//获取来源
				findTreeAll().then((res) => {
					setcommon("sourceList", res.data)
				});

				//获取课包
				findAllByCurriculumList().then((res) => {
					setcommon("curriculumList", res.data);
				});

				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index')
				})
			},
			async getOrgListbyuserid() {
				this.statusList = []
				const {
					success,
					msg,
					data
				} = await getOrgListbyuserid();

				if (success) {
					console.log(data);
					for (let s of data) {
						this.statusList.push({
							text: s.orgName,
							value: s.id,
							orgName: s.orgName,
							id: s.id,
						})
					}
				}
			},

			close() {
				Object.assign(this.$data, this.$options.data());
				this.$refs.popup.close()
				// uni.showTabBar();
				this.$emit('refresh')
			},
		}
	}
</script>

<style>
</style>